<template>
	<view>
		<header-top :messageInfo="{typeFlag:false,message:'登录',liner:false}"></header-top>
		<view class="mainpadding">
			<view class="logo">
				<image src="../../static/image/system/logo.png" mode=""></image>
			</view>
			<view class="xiahuax"></view>
			<view class="margin_top6 titletext">申请获取以下权限</view>
			<view class="huisetext margin_top4">获得你的公开信息（昵称，头像，手机号等）</view>
			<button class="bashi bigbtn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">授权登录</button>
			<view class="margin_top6 huisebtn">暂不登录</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
			data() {
				return {
					third_id:"",
					code:""
				}
			},
			/**
			 * 生命周期函数--监听页面显示
			 */
			onShow: function() {
				this.wxlogin()
			},
			methods: {
				getPhoneNumber(e) {
					var _this = this;
					let version = uni.getSystemInfoSync().SDKVersion;
					console.log(e,"获取手机号信息")
					if(e.detail.errMsg == "getPhoneNumber:ok"){
						// if(_this.compareVersion(version,'2.21.2')>=0){
						// 	_this.phoneNumber(_this,{code: e.detail.code})
						// }else{
							uni.login({
								provider: 'weixin',
								success: (auth) => {
									_this.phoneNumber(_this,{code: e.detail.code,iv: e.detail.iv,encryptedData:e.detail.encryptedData})
								},
								fail: (err) => {
									console.log(err,"错误信息")
									uni.showToast({'title':'微信登录授权失败',icon:"none"});
								}
							})
						// }
					}else{
						console.log("用户点击了拒绝")
					}
				},
				phoneNumber(that,data){
					data['third_id'] = this.third_id;
					httpRequest.request('api/user/get_mobile', 'POST', data).then(resp => {
						if (resp.code == 0) {
							console.log('获取code值失败')
							// httpRequest.toast(resp.msg);
							httpRequest.toast(resp.msg);
						} else if (resp.code == 1) {
							this.bangding()
							console.log('获取code值成功')
							httpRequest.toast("登录成功", 2000, true);
							setTimeout(() => {
								let userinfo = resp['data']['userinfo'];
								uni.setStorageSync('token', resp['data']['userinfo']['token']);
								uni.setStorageSync('user_id', resp['data']['userinfo']['id']);
								uni.navigateBack(1);
							}, 1000)
						}
					}).catch(err=>{
						console.log(err,"错误信息")
					})
					
				},
				bangding(){
					let lastuser_id = uni.getStorageSync("lastuser_id")
					if(lastuser_id){
						httpRequest.request('api/user/bind_relation', 'POST', {
							scene:lastuser_id
						}).then(resp => {
							console.log(resp,"绑定接口")
						}).catch(err=>{
							console.log(err,"错误信息")
						})
					}
				},
				//版本比较
				compareVersion(v1, v2) {
				  v1 = v1.split('.')
				  v2 = v2.split('.')
				  const len = Math.max(v1.length, v2.length)
				
				  while (v1.length < len) {
				    v1.push('0')
				  }
				  while (v2.length < len) {
				    v2.push('0')
				  }
				
				  for (let i = 0; i < len; i++) {
				    const num1 = parseInt(v1[i])
				    const num2 = parseInt(v2[i])
				
				    if (num1 > num2) {
				      return 1
				    } else if (num1 < num2) {
				      return -1
				    }
				  }
				
				  return 0
				},
				wxlogin() {
					let _this = this
					uni.login({
						success(res) {
							console.log(res,"yemian ")
							if (res.code) {
								_this.code = res.code
								uni.setStorageSync('code', res.code);
								httpRequest.request('/api/user/wxlogin', 'POST', {
									code:res.code
								}).then(resp => {
									console.log(resp,8989)
									_this.third_id = resp.data.third.third_id
								}).catch(err=>{
									console.log(err,"获取微信信息")
								})
							}
						}
					})
				},
				// 暂时不登陆
				closeLogin() {
					uni.navigateBack(1)
				}
	
			}
		}
</script>

<style lang="scss" scoped>
	.huisebtn{
		border-radius: 86rpx;
		height:80rpx;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #C5C5C5;
		font-size: 34rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
	}
	.bashi {
		margin-top: 80rpx;
	}

	.huisetext {
		font-size: 30rpx;
		font-family: PingFang SC-Medium, PingFang SC;
		font-weight: 500;
		color: #999999;
	}

	.xiahuax {
		margin-top: 60rpx;
		width: 100%;
		height: 2rpx;
		background-color: #CDCDCD;
	}

	.logo {
		margin: 30rpx auto 0;
		width: 297rpx;
		height: 267rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}
</style>
